//创建
var treeid="";
$("#orgSources").append("<li id='indexOrg'> <a href='#'><i class='zmdi zmdi-home'></i> 组织机构</a></li>");

//根据点选的组织机构，重新加载用户列表
var url, id;
function showUser(url, id) {
    
}

//组织机构树形块初始化
$.ajax({
    type: "POST",
    url: "/platform/windows/org/showOrgTree",
    dataType: "json",
    async: false,
    success: function (data) {
    	
        if (data.message == "ok") {
            //获取经过权限验证后的菜单json
            var json = data.orgs;
            //对菜单树进行排列
            createTree(json,1);
        }
        else {

        }
    }
});

//递归生成组织机构
function createTree(json,step)
{
	if(step==7)
	{
		return ;
	}
	for (var i = 0; i < json.length; i++) {
		if($("#li" + json[i]["id"]).size()==0)
		{
			if (json[i]["parent_org"] == null) {
	            $("#orgSources").append("<li id='li"+ json[i]["id"] + "'> <a href='#'><i class='zmdi zmdi-menu'></i> <span>" + json[i]["name"] + "</span></a></li>");
	        }
	        if(json[i]["parent_org"] != null&&$("#li" + json[i]["parent_org"]).has("ul").length == 0)
	        {
	        	$("#li" + json[i]["parent_org"]).attr("class", "sub-menu");
	            $("#li" + json[i]["parent_org"]).append("<ul id='ul" + json[i]["parent_org"] + "'></ul>");
	        }
	        if(json[i]["parent_org"] != null&&$("#li" + json[i]["parent_org"]).has("ul").length != 0)
	        {
	        	$("#ul" + json[i]["parent_org"]).append("<li id='li" + json[i]["id"] + "'><a href='javascript:makeSelected(" + json[i]["id"] + ");'>" + json[i]["name"] + "</a></li>");
	        }
		}
    }
	createTree(json,step*1+1);
}

//关闭树
function closeTree()
{
	var selected=$('#orgTree-trigger');
    $('.sub-menu.toggled').not('.active').each(function () {
    	selected.removeClass('toggled');
    	selected.find('ul').hide();
    });
    setTimeout(function () {
        $('#orgTree').removeClass('toggled');
        $('#orgTree-trigger').removeClass('open');
    });
}

